
using Android.Support.V4.App;
using Android.OS;
using Android.Views;

namespace LePcCtrlApp
{

    public class MouseFragment : Fragment, View.IOnTouchListener, GestureDetector.IOnGestureListener
    {
        private GestureDetector m_GestureDetector;
        private LoginManager m_LoginManager;
        private MouseEventPacket m_MouseEventPacket;
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Create your fragment here
            m_LoginManager = LoginManager.GetInstance();
            m_MouseEventPacket = new MouseEventPacket();
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment           
            View view= inflater.Inflate(Resource.Layout.MouseLayout, container, false);
            m_GestureDetector = new GestureDetector(view.Context,this); 
            view.SetOnTouchListener(this);
            return view;
        }

        public bool OnDown(MotionEvent e)
        {
            return true;
        }

        public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {           
            return true;
        }

        public void OnLongPress(MotionEvent e)
        {
            m_MouseEventPacket.UpAllKey();
            m_MouseEventPacket.KeyDown(MouseEventPacket.MouseKey.rigth);
            m_LoginManager.SendPacketAsync(m_MouseEventPacket.GetMouseEventPacket());
            m_MouseEventPacket.UpAllKey();
            m_MouseEventPacket.KeyUp(MouseEventPacket.MouseKey.rigth);
            m_LoginManager.SendPacketAsync(m_MouseEventPacket.GetMouseEventPacket());
        }
       

        public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
        {
            m_MouseEventPacket.Move((short)(-distanceX), (short)(-distanceY));
            m_LoginManager.SendPacketAsync(m_MouseEventPacket.GetMouseEventPacket());
            return true;
        }

        public void OnShowPress(MotionEvent e)
        {
           
        }

        public bool OnSingleTapUp(MotionEvent e)
        {
            m_MouseEventPacket.UpAllKey();
            m_MouseEventPacket.KeyDown(MouseEventPacket.MouseKey.Left);
            m_LoginManager.SendPacketAsync(m_MouseEventPacket.GetMouseEventPacket());
            m_MouseEventPacket.UpAllKey();
            m_MouseEventPacket.KeyUp(MouseEventPacket.MouseKey.Left);
            m_LoginManager.SendPacketAsync(m_MouseEventPacket.GetMouseEventPacket());
            return true;
        }

        public bool OnTouch(View v, MotionEvent e)
        {
           return m_GestureDetector.OnTouchEvent(e);
        }
    }
}